package cn.b.sky.mes.pp.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.pp.model.ProductionControl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.andon.enums.AndonConfigType;
import cn.b.sky.mes.andon.model.AndonConfig;
import cn.b.sky.mes.andon.service.AndonConfigService;
import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.cm.service.WorkCenterService;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.message.service.MessageService;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.pp.enums.ProductionControlStatusCode;
import cn.b.sky.mes.pp.service.ProductionControlService;
import cn.b.sky.mes.pp.service.RequirementStrategyService;

@Controller
@RequestMapping(value = "/andonPull/*")
public class AndonPullController extends SecurityController {
  @Resource
  WorkCenterService workCenterService;

  @Resource
  RequirementStrategyService requirementStrategyService;

  @Resource
  AndonConfigService andonConfigService;

  @Resource
  MessageService messageService;

  @Resource
  private ProductionControlService productionControlService;

  @RequestMapping(value = "andonPull")
  public String andonPull(Model model, String navTabId) {
    List<WorkCenter> workcenters = workCenterService.getAllWorkCenterListBySiteCode(getSiteCode(), 0);
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("workcenters", workcenters);
    return "mm/andon/andon_pull";
  }

  @RequestMapping(value = "getAndons")
  @ResponseBody
  public DWZResult getAndons(Model model, String navTabId, HttpServletRequest request, AndonConfig andonConfig) {
    List<AndonConfig> andons = null;
    if (andonConfig == null) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    andonConfig.setSiteCode(getSiteCode());
    ProductionControl productionControl = productionControlService.selectProductionControl(getSiteCode(),
        andonConfig.getProductionControlNum());
    if (!productionControl.getStatuseCode().equals(ProductionControlStatusCode.PRODUCT.getValue())
        && !productionControl.getStatuseCode().equals(ProductionControlStatusCode.SUBMIT.getValue())) {
      return ajaxError(getImeViewMessageI18n(null, "andonProductControlError.M"));
    }
    if (!StringUtils.isBlank(andonConfig.getWorkUnitCode())) {
      andons = andonConfigService.getAndonConfigByCode(AndonConfigType.WORKUNIT.getValue(), getSiteCode(),
          andonConfig.getWorkUnitCode());
    } else {
      andons = andonConfigService.getAndonConfigByCode(AndonConfigType.WORKCENTER.getValue(), getSiteCode(),
          andonConfig.getWorkCenterCode());
    }
    if (andons != null) {
      for (AndonConfig andon : andons) {
        andon.setWorkCenterCode(andonConfig.getWorkCenterCode());
        andon.setWorkUnitCode(andonConfig.getWorkUnitCode());
        andon.setProductionControlNum(andonConfig.getProductionControlNum());
        andon.setProcessOperationId(andonConfig.getProcessOperationId());
      }
    }
    return ajaxDone(andons);

  }

  @RequestMapping(value = "createRequirement")
  @ResponseBody
  public DWZResult createRequirement(Model model, String navTabId, HttpServletRequest request,
      AndonConfig andonConfig) {
    if (andonConfig == null) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    if (requirementStrategyService.isPullStrategyEnable()) {
      andonConfig.setSiteCode(getSiteCode());
      requirementStrategyService.createAndonRequirement(andonConfig);
    }
    return ajaxDone(andonConfig);

  }

  @RequestMapping(value = "sendMessage")
  @ResponseBody
  public DWZResult sendMessage(Model model, String navTabId, HttpServletRequest request, String messageCode) {
    if (messageCode == null) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    messageService.sendMessage(getSiteCode(), messageCode, null);
    return ajaxDone(messageCode);

  }
}
